home *** CD-ROM | disk | FTP | other *** search
/ LG Super CD / LG Super CD.iso / bitpim / bitpim-0.62-setup.exe / {app} / bitpim.exe / getopt.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-11-06  |  4.8 KB  |  160 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.3)
  3.  
  4. __all__ = [
  5.     'GetoptError',
  6.     'error',
  7.     'getopt',
  8.     'gnu_getopt']
  9. import os
  10.  
  11. class GetoptError(Exception):
  12.     opt = ''
  13.     msg = ''
  14.     
  15.     def __init__(self, msg, opt = ''):
  16.         self.msg = msg
  17.         self.opt = opt
  18.         Exception.__init__(self, msg, opt)
  19.  
  20.     
  21.     def __str__(self):
  22.         return self.msg
  23.  
  24.  
  25. error = GetoptError
  26.  
  27. def getopt(args, shortopts, longopts = []):
  28.     opts = []
  29.     if type(longopts) == type(''):
  30.         longopts = [
  31.             longopts]
  32.     else:
  33.         longopts = list(longopts)
  34.     while args and args[0].startswith('-') and args[0] != '-':
  35.         if args[0] == '--':
  36.             args = args[1:]
  37.             break
  38.         
  39.         if args[0].startswith('--'):
  40.             (opts, args) = do_longs(opts, args[0][2:], longopts, args[1:])
  41.             continue
  42.         (opts, args) = do_shorts(opts, args[0][1:], shortopts, args[1:])
  43.     return (opts, args)
  44.  
  45.  
  46. def gnu_getopt(args, shortopts, longopts = []):
  47.     opts = []
  48.     prog_args = []
  49.     if isinstance(longopts, str):
  50.         longopts = [
  51.             longopts]
  52.     else:
  53.         longopts = list(longopts)
  54.     if shortopts.startswith('+'):
  55.         shortopts = shortopts[1:]
  56.         all_options_first = True
  57.     elif os.environ.get('POSIXLY_CORRECT'):
  58.         all_options_first = True
  59.     else:
  60.         all_options_first = False
  61.     while args:
  62.         if args[0] == '--':
  63.             prog_args += args[1:]
  64.             break
  65.         
  66.         if args[0][:2] == '--':
  67.             (opts, args) = do_longs(opts, args[0][2:], longopts, args[1:])
  68.             continue
  69.         if args[0][:1] == '-':
  70.             (opts, args) = do_shorts(opts, args[0][1:], shortopts, args[1:])
  71.             continue
  72.         if all_options_first:
  73.             prog_args += args
  74.             break
  75.             continue
  76.         prog_args.append(args[0])
  77.         args = args[1:]
  78.     return (opts, prog_args)
  79.  
  80.  
  81. def do_longs(opts, opt, longopts, args):
  82.     
  83.     try:
  84.         i = opt.index('=')
  85.     except ValueError:
  86.         optarg = None
  87.  
  88.     (opt, optarg) = (opt[:i], opt[i + 1:])
  89.     (has_arg, opt) = long_has_args(opt, longopts)
  90.     if has_arg:
  91.         if optarg is None:
  92.             if not args:
  93.                 raise GetoptError('option --%s requires argument' % opt, opt)
  94.             
  95.             (optarg, args) = (args[0], args[1:])
  96.         
  97.     elif optarg:
  98.         raise GetoptError('option --%s must not have an argument' % opt, opt)
  99.     
  100.     if not optarg:
  101.         pass
  102.     opts.append(('--' + opt, ''))
  103.     return (opts, args)
  104.  
  105.  
  106. def long_has_args(opt, longopts):
  107.     possibilities = []
  108.     if not possibilities:
  109.         raise GetoptError('option --%s not recognized' % opt, opt)
  110.     
  111.     if opt in possibilities:
  112.         return (False, opt)
  113.     elif opt + '=' in possibilities:
  114.         return (True, opt)
  115.     
  116.     if len(possibilities) > 1:
  117.         raise GetoptError('option --%s not a unique prefix' % opt, opt)
  118.     
  119.     unique_match = possibilities[0]
  120.     has_arg = unique_match.endswith('=')
  121.     if has_arg:
  122.         unique_match = unique_match[:-1]
  123.     
  124.     return (has_arg, unique_match)
  125.  
  126.  
  127. def do_shorts(opts, optstring, shortopts, args):
  128.     while optstring != '':
  129.         (opt, optstring) = (optstring[0], optstring[1:])
  130.         if short_has_arg(opt, shortopts):
  131.             if optstring == '':
  132.                 if not args:
  133.                     raise GetoptError('option -%s requires argument' % opt, opt)
  134.                 
  135.                 (optstring, args) = (args[0], args[1:])
  136.             
  137.             (optarg, optstring) = (optstring, '')
  138.         else:
  139.             optarg = ''
  140.         opts.append(('-' + opt, optarg))
  141.     return (opts, args)
  142.  
  143.  
  144. def short_has_arg(opt, shortopts):
  145.     for i in range(len(shortopts)):
  146.         if shortopts[i] == shortopts[i]:
  147.             pass
  148.         elif shortopts[i] != ':':
  149.             return shortopts.startswith(':', i + 1)
  150.             continue
  151.     
  152.     raise GetoptError('option -%s not recognized' % opt, opt)
  153.  
  154. if __name__ == '__main__':
  155.     import sys
  156.     print getopt(sys.argv[1:], 'a:b', [
  157.         'alpha=',
  158.         'beta'])
  159.  
  160.